#include <stdio.h>
#include <string.h>
#include <malloc.h>


int bin2dec(char* strBin);
int findLenghTer(int value);
void dec2tern(int dec, int tam, char* retorno);


int main (int argc, char** argv){
	char strBin[10];
	int lengh ;
	
	
	
	printf("\n Put bellow the binary value to be converted into ternary begining with an 's' character:\n");
	scanf("%s", strBin);
	printf("\n The entered string was: %s", strBin);
	
	int binValue = bin2dec(strBin);
	printf("/n binValue = %d/n", binValue);
	
	lengh = findLenghTer(binValue);
	char terNotationStr[lengh];
	
	dec2tern(binValue, lengh, terNotationStr);
	
	printf("%s", terNotationStr);
	
}

int bin2dec(char* strBin){
	int decValue = 0;
	for(int i=1; i< strlen(strBin)+1; i++){
		decValue += strBin[i]%0x30 * 2^(5-i);
	}
	return decValue;
}

int findLenghTer(int value){
	int tam = 1;
	while(value > 3){
		value /= 3;
		tam++;
	}
	return tam;
}

void dec2tern(int dec, int tam, char* ternary){
	int quo, rest[tam], idx = 0;
	
	while(quo >=0 ){
		quo = dec/3;
		rest[idx] = dec%3;
		idx++;
	}
	
	for(int i=0; i<tam; i++){
		ternary[i] = rest[i]+0x30;
	}
}
